package com.five.novel.controller;

import com.five.basic.result.PageList;
import com.five.novel.mapper.NovelCategoryMapper;
import com.five.novel.service.INovelCategoryService;
import com.five.pojo.NovelCategory;
import com.five.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/novelCategory")
public class NovelCategoryController {

    @Autowired
    private INovelCategoryService novelCategoryService;



    /*
    * 根据男女类型分页查询所有小说
    * */
    @PostMapping("/queryByType")
    public Result queryByType(@RequestBody Long type) {
        try {
            PageList<NovelCategory> list = novelCategoryService.queryByType(type);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }


    /*
    * 查询小说分类树
    * */
    @PostMapping("/novelCategoryTree")
    public Result tree(@RequestBody Long type) {
        try {
            List<NovelCategory> list = novelCategoryService.novelCategoryTree(type);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }


    /*
     * 添加小说分类
     */
    @PostMapping("/add")
    public Result add(@RequestBody NovelCategory novelCategory) {
        try {
            novelCategoryService.add(novelCategory);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error("添加失败");
        }
    }


    /*
     * 更新小说分类
     */
    @PostMapping("/update")
    public Result update(@RequestBody NovelCategory novelCategory) {
        try {
            novelCategoryService.update(novelCategory);
            return Result.success("更新成功");
        } catch (Exception e) {
            return Result.error("更新失败");
        }
    }


    /*
     * 删除小说分类
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        try {
            novelCategoryService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }


    /*
     * 批量删除小说分类
     */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        try {
            novelCategoryService.deleteBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            return Result.error("批量删除失败");
        }
    }


    /*
     * 根据ID查询小说分类
     */
    @PostMapping("/queryById")
    public Result queryById(@RequestBody Long id) {
        try {
            NovelCategory novelCategory = novelCategoryService.queryById(id);
            return Result.success(novelCategory);
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }

    /*
     * 查询所有小说分类
     */
    @PostMapping("/queryAll")
    public Result queryAll() {
        try {
            List<NovelCategory> list = novelCategoryService.queryAll();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }

    /*`
     * 分页查询小说分类
     */
    @PostMapping("/queryPage")
    public Result queryPage(@RequestBody NovelCategory novelCategory) {
        try {
            PageList<NovelCategory> list = novelCategoryService.queryPage(novelCategory);
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("查询失败");
        }
    }
}